package androidgames.data;

import java.io.IOException;
import java.util.ArrayList;

import javax.xml.parsers.ParserConfigurationException;
import javax.xml.parsers.SAXParser;
import javax.xml.parsers.SAXParserFactory;

import org.xml.sax.InputSource;
import org.xml.sax.SAXException;
import org.xml.sax.XMLReader;

import android.util.Log;

public class ReadXML {
	ArrayList<gameFile> listFileServer = null;

	/** constructor */
	public ReadXML() {
		listFileServer = new ArrayList<gameFile>();
	}

	/** return list of file from server */
	public ArrayList<gameFile> read(String path) {
		try {
			SAXParserFactory spf = SAXParserFactory.newInstance();
			SAXParser sp = spf.newSAXParser();
			XMLReader xr = sp.getXMLReader();
			Handler myHandler = new Handler();
			xr.setContentHandler(myHandler);
			xr.parse(new InputSource(path));
			myHandler.startDocument();
			listFileServer = myHandler.getListFile();
			Log.d("list size: ", ""+ listFileServer.size());
			return listFileServer;
		} catch (IOException e) {
			Log.d("IOException: ", e.toString());
		} catch (SAXException e) {
			Log.d("SAXException: ", e.toString());
		} catch (ParserConfigurationException e) {
			Log.d("ParserConfigurationException: ", e.toString());
		}
		return null;
	}
}
